package dwdddhhhhh;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import HashMap.Data;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Iterator;

public class InsertPanel extends JPanel 
{
	private JLabel lblId;
	private JTextField tF_ID;
	private JLabel lblName;
	private JTextField tF_Name;
	private JLabel lblBirthday;
	private JTextField tF_Birth;
	private JLabel lblAddress;
	private JTextField tF_Address;
	private JButton btnInsert;
	private JLabel label;
	private JButton btnSelect;
	private JLabel label_ID;

	/**
	 * Create the panel.
	 */
	public InsertPanel() 
	{
		setLayout(null);
		add(getLblId());
		add(getTF_ID());
		add(getLblName());
		add(getTF_Name());
		add(getLblBirthday());
		add(getTF_Birth());
		add(getLblAddress());
		add(getTF_Address());
		add(getBtnInsert());
		add(getLabel());
		add(getBtnSelect());
		add(getLabel_ID());
	}
	private JLabel getLblId() 
	{
		if (lblId == null) 
		{
			lblId = new JLabel("ID : ");
			lblId.setBounds(27, 67, 57, 15);
		}
		return lblId;
	}
	private JTextField getTF_ID() 
	{
		if (tF_ID == null) 
		{
			tF_ID = new JTextField();
			tF_ID.setBounds(105, 64, 221, 21);
			tF_ID.setColumns(10);
		}
		return tF_ID;
	}
	
	private JLabel getLblName() 
	{
		if (lblName == null) 
		{
			lblName = new JLabel("Name : ");
			lblName.setBounds(27, 122, 57, 15);
		}
		return lblName;
	}
	
	private JTextField getTF_Name() 
	{
		if (tF_Name == null) 
		{
			tF_Name = new JTextField();
			tF_Name.setBounds(105, 119, 316, 21);
			tF_Name.setColumns(10);
		}
		return tF_Name;
	}
	
	private JLabel getLblBirthday() 
	{
		if (lblBirthday == null) 
		{
			lblBirthday = new JLabel("BirthDay : ");
			lblBirthday.setBounds(27, 153, 67, 15);
		}
		return lblBirthday;
	}
	
	private JTextField getTF_Birth() 
	{
		if (tF_Birth == null) 
		{
			tF_Birth = new JTextField();
			tF_Birth.setBounds(105, 150, 316, 21);
			tF_Birth.setColumns(10);
		}
		return tF_Birth;
	}
	
	private JLabel getLblAddress() 
	{
		if (lblAddress == null) 
		{
			lblAddress = new JLabel("Address : ");
			lblAddress.setBounds(27, 188, 67, 15);
		}
		return lblAddress;
	}
	
	private JTextField getTF_Address() 
	{
		if (tF_Address == null) 
		{
			tF_Address = new JTextField();
			tF_Address.setBounds(105, 185, 316, 21);
			tF_Address.setColumns(10);
		}
		return tF_Address;
	}
	
	private JButton getBtnInsert() 
	{
		if (btnInsert == null) 
		{
			btnInsert = new JButton("Insert");
			btnInsert.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					String id = tF_ID.getText();
					String name = tF_Name.getText();
					String birthday = tF_Birth.getText();
					String address = tF_Address.getText();
					
					MapFrame.map.put(id, new Data(id, name, birthday, address));
					
					label.setText("Insert SUCCESS");
					
					tF_ID.setText("");
					tF_Name.setText("");
					tF_Birth.setText("");
					tF_Address.setText("");
					label_ID.setText("");
				}
			});
			btnInsert.setBounds(27, 216, 394, 32);
		}
		return btnInsert;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("");
			label.setBounds(27, 258, 394, 32);
		}
		return label;
	}
	private JButton getBtnSelect() 
	{
		if (btnSelect == null) 
		{
			btnSelect = new JButton("Select");
			btnSelect.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					String id = tF_ID.getText();
					
					Iterator<String> key = MapFrame.map.keySet().iterator();
					Iterator<Data> values = MapFrame.map.values().iterator();

					while (key.hasNext()) 
					{
						String idKey = key.next();
						if (idKey.equalsIgnoreCase(id)) 
						{
							label_ID.setText(id+" is already EXIST");
							tF_ID.setText("");
							break;
						}
						else 
						{
							label_ID.setText(id+" is USABLE ID");
							tF_ID.setText(id);
						}
					}
				}
			});
			btnSelect.setBounds(340, 63, 81, 23);
		}
		return btnSelect;
	}
	private JLabel getLabel_ID() {
		if (label_ID == null) {
			label_ID = new JLabel("");
			label_ID.setBounds(105, 95, 316, 15);
		}
		return label_ID;
	}
}
